/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.cxhd.lis.util;

import java.security.MessageDigest;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Administrator
 */
public class Tool {
    public static List<Map<String, String>> convertList(ResultSet rs) throws SQLException {

        List <Map<String, String>> list = new ArrayList<Map<String, String>>();

        ResultSetMetaData md = rs.getMetaData();

        int columnCount = md.getColumnCount(); //Map rowData;  

        while (rs.next()) { //rowData = new HashMap(columnCount);  

            Map <String, String> rowData = new HashMap<String, String>();

            for (int i = 1; i <= columnCount; i++) {
              //  System.out.println(rs.getObject(i));
                rowData.put(md.getColumnName(i), rs.getString(i));

            }

            list.add(rowData);

        }
        return list;

    }
    
     public static String string2MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++) {
            byteArray[i] = (byte) charArray[i];
        }
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();

    }

    /**
     * 加密解密算法 执行一次加密，两次解密
     */
    public static String convertMD5(String inStr) {

        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) (a[i] ^ 't');
        }
        String s = new String(a);
        return s;

    }
    
    public static String getSampleNo(String code,String sample) {

        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(sample);
        String sam = m.replaceAll("").trim();
        if (sam.length() == 1) {
            sam = "000" + sam;
        } else if (sam.length() == 2) {
            sam = "00" + sam;
        }
         else if (sam.length() == 3) {
            sam = "0" + sam;
        }
        return code+sam;
    }
}
